package com.sina.interfaces.manage.config;

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sina.interfaces.core.util.JsonUtils;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

/**
 * 专门用于配置 json 视图
 * created  on 06/06/2017.
 */
@Configuration("jsonViewContextConfig")
public class JsonViewContextConfig {

    @Bean
    public static ObjectMapper jsonViewObjectMapper(@Qualifier("interfaceSessionFactory") SessionFactory sessionFactory) {
        return JsonUtils.createDefaultObjectMapper(sessionFactory);
    }

    @Bean
    public static MappingJackson2JsonView resultView(@Qualifier("jsonViewObjectMapper")
                                                             ObjectMapper objectMapper) {
        MappingJackson2JsonView view = new MappingJackson2JsonView(objectMapper);
        view.setExtractValueFromSingleKeyModel(true);
        view.setModelKey("result");
        view.setEncoding(JsonEncoding.UTF8);
        return view;
    }

}
